home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Periodicals / develop / develop 7 code / QuickTime / SimpleInMovies / SimpleInUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-13  |  11.4 KB  |  444 lines  |  [TEXT/MPS ]

  1.  
  2. /*     
  3. SimpleInMovies
  4.  
  5. Sample programs demonstrating how to open and display
  6. QuickTime™ Movies.
  7.  
  8. SimpleInUtils.c file contains the code for the routines 
  9. called upon when dealing with the dialogs used to save movies.
  10. These calls came almost untouched from the DTS Utilities files,
  11. note though that some were edited to make them fit the needs of this
  12. sample.
  13.  
  14. Guillermo A. Ortiz
  15. Macintosh Developer Technical Support
  16.  
  17. 08/13/91 Created the file to move in utilities calls.
  18. 11/19/91 Added stuff to have custom dialog in PutFile to handle flattening.
  19. */
  20.  
  21. #include <SimpleInMovie.h>
  22.  
  23. long gFlattenMovies = 1;
  24.  
  25. DialogPtr GetCenteredDialog(short, WindowPtr, WindowPtr);
  26. void OutlineDialogItem(DialogPtr, short);
  27. pascal Boolean    keyEquivFilter(DialogPtr, EventRecord *, short *);
  28. Handle    GetAppResource(ResType,short, OSErr *);
  29. Boolean DisplayPutFile(StandardFileReply *);
  30. void    OutlineControl(ControlHandle);
  31. Rect    CenterWindow(WindowPtr, WindowPtr);
  32. short    GetHexByte(char *);
  33. char    LockHandleHigh(Handle);
  34. GDHandle    GetRectDevice(Rect);
  35. Rect    GetWindowDeviceRectNMB(WindowPtr);
  36. Rect    GetWindowDeviceRect(WindowPtr window);
  37. Rect    GetMainScreenRect(void);
  38. Rect    GetWindowStructureRect(WindowPtr);
  39. Point    GetGlobalTopLeft(WindowPtr);
  40. void    PositionRectInRect(Rect, Rect *, Fixed , Fixed);
  41. Rect    GetWindowContentRect(WindowPtr window);
  42. void    LocalToGlobalRect(Rect *);
  43. void    pstrcat(char *, char *);
  44. void    pstrcpy(char *, char *);
  45.  
  46. /*****************************************************************************/
  47.  
  48. DialogPtr    GetCenteredDialog(short id, WindowPtr relatedWindow, WindowPtr behind)
  49. {
  50.     DialogTHndl        dlogResource;
  51.     DialogPtr        dialog;
  52.     Boolean            oldVis;
  53.     char            hstate;
  54.     OSErr            err;
  55.     
  56.     dialog = nil;
  57.     if (dlogResource = (DialogTHndl)GetAppResource('DLOG', id, &err)) {
  58.         hstate = LockHandleHigh((Handle)dlogResource);
  59.         oldVis = (*dlogResource)->visible;
  60.         (*dlogResource)->visible = false;
  61.         if (dialog = GetNewDialog(id, nil, behind)) {
  62.             CenterWindow(dialog, relatedWindow);
  63.             if (oldVis) ShowWindow(dialog);
  64.         }
  65.         (*dlogResource)->visible = oldVis;
  66.         HSetState((Handle)dlogResource, hstate);
  67.     }
  68.     return(dialog);
  69. }
  70.  
  71. Handle    GetAppResource(ResType theType,short theID, OSErr *err)
  72. {
  73.     Handle    returnHandle;
  74.     
  75.     returnHandle = Get1Resource(theType, theID);
  76.     *err = ResError();
  77.     return (returnHandle);
  78. }
  79.  
  80. /* The dialog hook proc processes the item hits in the custom put dialog;
  81.    this sample does the following:
  82.    
  83.    on sfHookFirstCall    : initializes the 'Flatten Movie' check box, the
  84.                           global flag controlling this starts with YES, flatten.
  85.                           Return sfHookNullEvent to signal the hit has been taken
  86.                           care of.
  87.                        
  88.    on kFlattenMovie        : the user clicked on the box change it to the new state.
  89.                           Return sfHookNullEvent to signal the hit has been taken
  90.                           care of.
  91.    
  92.    on sfItemOpenButton    : the user says ok, save the state of the flatten box
  93.                            so the next time it is used to init the dialog.
  94.                           Return sfItemOpenButton to let SF do its normal stuff.
  95. */
  96. pascal short MyDlogHook(short item, DialogPtr theDialog, Ptr myData)
  97. {
  98. short itemType, value;
  99. Handle dItem;
  100. Rect box;
  101.  
  102.     switch (item) {
  103.     case     sfHookFirstCall:
  104.       if (*(long *)myData) { /* check if flatten movie has been selected and mark box accordingly */
  105.         GetDItem(theDialog, kFlattenMovie, &itemType, &dItem, &box);
  106.           if (itemType == (ctrlItem+chkCtrl)) { /* is this our item? */
  107.           SetCtlValue((ControlHandle)dItem, 1); /* check the box, comes unchecked from resource */
  108.         }
  109.       }
  110.       return sfHookNullEvent;
  111.       break;
  112.         
  113.     case     kFlattenMovie:
  114.       GetDItem(theDialog, kFlattenMovie, &itemType, &dItem, &box);
  115.       if (itemType == (ctrlItem+chkCtrl)) { /* is this our item? */
  116.         value = GetCtlValue((ControlHandle)dItem);    /* get current setting     */
  117.         SetCtlValue((ControlHandle)dItem, value?0:1);    /* and switch it        */
  118.       }
  119.       return sfHookNullEvent;
  120.       break;
  121.     case sfItemOpenButton:
  122.       GetDItem(theDialog, kFlattenMovie, &itemType, &dItem, &box);
  123.       if (itemType == (ctrlItem+chkCtrl)) { /* is this our item? */
  124.         value = GetCtlValue((ControlHandle)dItem);    /* get current setting     */
  125.         *(long *)myData =value;
  126.       }
  127.     break;
  128.     default: 
  129.       break;
  130.     }
  131.     return item; /* in all other cases return the item to let SF do the normal processing */
  132. }
  133.  
  134. Boolean DisplayPutFile(reply)
  135. StandardFileReply *reply;
  136. {
  137.     Str255        prompt;
  138.     Point        where = {-1, -1};
  139.  
  140.     GetIndString(prompt, rMiscStrings, sSFprompt);
  141.  
  142.     CustomPutFile(prompt, reply->sfFile.name, reply, rCustomPut, where,
  143.                   MyDlogHook, nil, nil, nil, &gFlattenMovies);
  144.  
  145.     return(reply->sfGood);
  146. }
  147.  
  148. void    OutlineDialogItem(DialogPtr dlgPtr, short item)
  149. {
  150.     short iKind;
  151.     Handle iHandle;
  152.     Rect iRect;
  153.  
  154.     GetDItem (dlgPtr, item, &iKind, &iHandle, &iRect);
  155.     OutlineControl((ControlHandle) iHandle);
  156. }
  157.  
  158. void    OutlineControl(ControlHandle button)
  159. {
  160.     Rect        theRect;
  161.     PenState    curPen;
  162.     short        buttonOval;
  163.  
  164.     if (button) {
  165.         SetPort((*button)->contrlOwner);
  166.         GetPenState(&curPen);
  167.         PenNormal();
  168.         theRect = (*button)->contrlRect;
  169.         InsetRect(&theRect, kButtonFrameInset, kButtonFrameInset);
  170.         buttonOval = (theRect.bottom - theRect.top) / 2 + 2;
  171.  
  172.         PenPat((*button)->contrlHilite == kCntlActivate ? &(qd.black) : &(qd.gray));
  173.  
  174.         PenSize(kButtonFrameSize, kButtonFrameSize);
  175.         FrameRoundRect(&theRect, buttonOval, buttonOval);
  176.         SetPenState(&curPen);
  177.     }
  178. }
  179.  
  180.  
  181. pascal Boolean    keyEquivFilter(DialogPtr dlg, EventRecord *event, short *item)
  182. {
  183.     short    itemType;
  184.     Handle    itemHndl;
  185.     Rect    itemRect;
  186.     Str255    itemText;
  187.     short    i, theChr, theMod, equivChr, modMask, modVal, itemNum;
  188.     long    tick;
  189.  
  190.     if (event->what == updateEvt) {
  191.         if (dlg == (DialogPtr)event->message) OutlineDialogItem(dlg, 1);
  192.         return(false);
  193.     }
  194.  
  195.     if (event->what != keyDown) return(false);
  196.  
  197.     itemNum = 0;
  198.  
  199.     theChr = event->message   & charCodeMask;
  200.     theMod = event->modifiers & keyCodeMask;
  201.  
  202.     if ((theChr == 0x0D) || (theChr == 0x03)) {        /* If return or enter... */
  203.         if (!(theMod & (cmdKey + optionKey + controlKey))) itemNum = 1;
  204.     }        
  205.     else {
  206.  
  207.         GetDItem(dlg, 2, &itemType, &itemHndl, &itemRect);
  208.         GetIText(itemHndl, itemText);
  209.  
  210.         for (i = 1; i <= *itemText; i += 9) {
  211.             equivChr = GetHexByte(itemText + i);
  212.             modMask  = GetHexByte(itemText + i + 2) << 8;
  213.             modVal   = GetHexByte(itemText + i + 4) << 8;
  214.             itemNum  = GetHexByte(itemText + i + 6);
  215.             if (theChr == equivChr)
  216.                 if ((theMod & modMask) == modVal) break;
  217.             itemNum = 0;
  218.         }
  219.     }
  220.  
  221.     if (itemNum) {
  222.         GetDItem(dlg, itemNum, &itemType, &itemHndl, &itemRect);
  223.         HiliteControl((ControlHandle)itemHndl, 1);
  224.         tick = TickCount();
  225.         while (TickCount() < tick + 10);
  226.         HiliteControl((ControlHandle)itemHndl, 0);
  227.         *item = itemNum;
  228.         return(true);
  229.     }
  230.  
  231.     return(false);
  232. }
  233.  
  234. Rect    GetWindowDeviceRectNMB(WindowPtr window)
  235. {
  236.     Rect        deviceRect, tempRect;
  237.  
  238.     deviceRect = GetWindowDeviceRect(window);
  239.     tempRect = GetMainScreenRect();
  240.     if (EqualRect(&deviceRect, &tempRect)) deviceRect.top += GetMBarHeight();
  241.  
  242.     return(deviceRect);
  243. }
  244.  
  245. Rect    CenterWindow(WindowPtr window, WindowPtr relatedWindow)
  246. {
  247.     WindowPtr    whichDevice;
  248.     Rect        deviceRect, oldWindowRect, newWindowRect, contentRect;
  249.  
  250.     if (!(whichDevice = relatedWindow)) whichDevice = window;
  251.         /* If we have a window to center against, use the device for that window,
  252.         ** else use the device for the window that is getting centered. */
  253.  
  254.     deviceRect = GetWindowDeviceRectNMB(whichDevice);
  255.         /* We now have the rectangle of the device we want to center within. */
  256.  
  257.     oldWindowRect = newWindowRect = GetWindowStructureRect(window);
  258.  
  259.     PositionRectInRect(deviceRect, &newWindowRect, FixRatio (1, 2),
  260.                        FixRatio (1, 3));
  261.         /* Figure out the new window strucRect so we can compare it against
  262.         ** the old strucRect.  This will tell us how much to move the window. */
  263.  
  264.     contentRect = GetWindowContentRect(window);
  265.         /* Get where the window is now. */
  266.  
  267.     OffsetRect(&contentRect, newWindowRect.left - oldWindowRect.left,
  268.                             newWindowRect.top  - oldWindowRect.top);
  269.         /* Calculate the new content rect. */
  270.  
  271.     MoveWindow(window, contentRect.left, contentRect.top, false);
  272.         /* Move the window to the new location. */
  273.  
  274.     return(contentRect);
  275. }
  276.  
  277. GDHandle    GetRectDevice(Rect globalRect)
  278. {
  279.     long        area;
  280.     long        maxArea;
  281.     GDHandle    device;
  282.     GDHandle    deviceToReturn;
  283.     Rect        intersection;
  284.  
  285.     deviceToReturn = GetMainDevice();            /* Use as default choice. */
  286.     maxArea = 0;
  287.  
  288.     for (device = GetDeviceList(); device; device = GetNextDevice(device)) {
  289.         if (TestDeviceAttribute(device, screenDevice)
  290.           && TestDeviceAttribute(device, screenActive)
  291.           && SectRect(&globalRect, &((*device)->gdRect), &intersection)) {
  292.             area = (intersection.right - intersection.left) *
  293.                    (intersection.bottom - intersection.top);
  294.             if (area > maxArea) {
  295.                 deviceToReturn = device;
  296.                 maxArea = area;
  297.             }
  298.         }
  299.     }
  300.     return(deviceToReturn);
  301. }
  302.  
  303. Rect    GetWindowDeviceRect(WindowPtr window)
  304. {
  305. #pragma unused (window)
  306.  
  307.     return(GetMainScreenRect());
  308. }
  309.  
  310. Rect    GetMainScreenRect(void)
  311. {
  312.     GDHandle    mainDevice;
  313.  
  314.         mainDevice = GetMainDevice();
  315.         return((*mainDevice)->gdRect);
  316. }
  317.  
  318. short    GetHexByte(char *cptr)
  319. {
  320.     short    val, i, chr;
  321.  
  322.     for (val = 0, i = 0; i < 2; ++i) {
  323.         chr = cptr[i];
  324.         if (chr == '=') return(cptr[++i]);
  325.         if (chr > 'F') chr -= 0x20;
  326.         if (chr > '9') chr -= ('A' - '9' - 1);
  327.         val = (val << 4) + chr - '0';
  328.     }
  329.     return(val);
  330. }
  331.  
  332. char    LockHandleHigh(Handle theHandle)
  333. {
  334.     char    hstate;
  335.  
  336.     hstate = HGetState(theHandle);
  337.     MoveHHi(theHandle);
  338.     HLock(theHandle);
  339.     return(hstate);
  340. }
  341.  
  342. Rect    GetWindowStructureRect(WindowPtr window)
  343. {
  344. #define kOffscreenLoc 0x4000
  345.  
  346.     GrafPtr        oldPort;
  347.     Rect        structureRect;
  348.     Point        windowLoc;
  349.     
  350.     if (((WindowPeek)window)->visible)
  351.         structureRect = (*(((WindowPeek)window)->strucRgn))->rgnBBox;
  352.  
  353.     else {
  354.         GetPort(&oldPort);
  355.         SetPort(window);
  356.         windowLoc = GetGlobalTopLeft(window);
  357.         MoveWindow(window, windowLoc.h, kOffscreenLoc, false);
  358.         ShowHide(window, true);
  359.         structureRect = (*(((WindowPeek)window)->strucRgn))->rgnBBox;
  360.         ShowHide(window, false);
  361.         MoveWindow(window, windowLoc.h, windowLoc.v, false);
  362.         SetPort(oldPort);
  363.         OffsetRect(&structureRect, 0, windowLoc.v - kOffscreenLoc);
  364.     }
  365.     return(structureRect);
  366. }
  367.  
  368. Point    GetGlobalTopLeft(WindowPtr window)
  369. {
  370.     GrafPtr            oldPort;
  371.     Point            globalPt;
  372.     
  373.     GetPort(&oldPort);
  374.     SetPort(window);
  375.     globalPt = TopLeft(window->portRect);
  376.     LocalToGlobal(&globalPt);
  377.     SetPort(oldPort);
  378.     return(globalPt);
  379. }
  380.  
  381. void    PositionRectInRect(Rect outerRect, Rect *innerRect, Fixed horzRatio, Fixed vertRatio)
  382. {
  383.     short        outerRectHeight;
  384.     short        outerRectWidth;
  385.     short        innerRectHeight;
  386.     short        innerRectWidth;
  387.     short        yLocation;
  388.     short        xLocation;
  389.  
  390.     outerRectHeight = outerRect.bottom - outerRect.top;
  391.     outerRectWidth = outerRect.right - outerRect.left;
  392.  
  393.     innerRectHeight = innerRect->bottom - innerRect->top;
  394.     innerRectWidth = innerRect->right - innerRect->left;
  395.         yLocation = Fix2Long(FixMul(Long2Fix(outerRectHeight - innerRectHeight), vertRatio))
  396.             + outerRect.top;
  397.         xLocation = Fix2Long(FixMul(Long2Fix(outerRectWidth - innerRectWidth), horzRatio))
  398.             + outerRect.left;
  399.  
  400.     innerRect->top = yLocation;
  401.     innerRect->left = xLocation;
  402.     innerRect->bottom = yLocation + innerRectHeight;
  403.     innerRect->right = xLocation + innerRectWidth;
  404. }
  405.  
  406. Rect    GetWindowContentRect(WindowPtr window)
  407. {
  408.     WindowPtr    oldPort;
  409.     Rect        contentRect;
  410.  
  411.     GetPort(&oldPort);
  412.     SetPort(window);
  413.     contentRect = window->portRect;
  414.     LocalToGlobalRect(&contentRect);
  415.     SetPort(oldPort);
  416.     return(contentRect);
  417. }
  418.  
  419. void    LocalToGlobalRect(Rect *aRect)
  420. {
  421.     LocalToGlobal(&TopLeft(*aRect));
  422.     LocalToGlobal(&BotRight(*aRect));
  423. }
  424.  
  425. void    pstrcat(char *d, char *s)
  426. {
  427.     short    i;
  428.  
  429.     for (i = 0; i < s[0];)
  430.         d[++d[0]] = s[++i];
  431. }
  432.  
  433. void    pstrcpy(char *d, char *s)
  434. {
  435.     short    i;
  436.  
  437.     i = *s;
  438.     do {
  439.         d[i] = s[i];
  440.     } while (i--);
  441. }
  442.  
  443. /*****************************************************************************/
  444.